package zozopo.manual.operations;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;


public class ContentImagesOS extends OperatingService{
	
	
	public ContentImagesOS(Connection conn){
		super(conn);
	}
	
	public ContentImagesOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ContentImagesTB> selectListBySql(String sql){
		List<ContentImagesTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);

		if(resultSet != null){
				try {
					list = new ArrayList<ContentImagesTB>();
					while(resultSet.next()){
						ContentImagesTB contentImage = new ContentImagesTB();
						
						contentImage.setId(resultSet.getInt(ContentImagesTB.ID));
						contentImage.setContent_id(resultSet.getInt(ContentImagesTB.CONTENT_ID));
						contentImage.setUser_id(resultSet.getInt(ContentImagesTB.USER_ID));
						
						contentImage.setImage_url_1(resultSet.getString(ContentImagesTB.IMAGE_URL_1));
						contentImage.setImage_url_2(resultSet.getString(ContentImagesTB.IMAGE_URL_2));
						contentImage.setImage_url_3(resultSet.getString(ContentImagesTB.IMAGE_URL_3));
						contentImage.setImage_url_4(resultSet.getString(ContentImagesTB.IMAGE_URL_4));
						contentImage.setImage_url_5(resultSet.getString(ContentImagesTB.IMAGE_URL_5));
						contentImage.setImage_url_6(resultSet.getString(ContentImagesTB.IMAGE_URL_6));
						contentImage.setImage_url_7(resultSet.getString(ContentImagesTB.IMAGE_URL_7));
						contentImage.setImage_url_8(resultSet.getString(ContentImagesTB.IMAGE_URL_8));
						contentImage.setImage_url_9(resultSet.getString(ContentImagesTB.IMAGE_URL_9));
						contentImage.setImage_url_10(resultSet.getString(ContentImagesTB.IMAGE_URL_10));
						contentImage.setImage_url_11(resultSet.getString(ContentImagesTB.IMAGE_URL_11));
						contentImage.setImage_url_12(resultSet.getString(ContentImagesTB.IMAGE_URL_12));
						
						contentImage.setSmall_image_url_1(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_1));
						contentImage.setSmall_image_url_2(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_2));
						contentImage.setSmall_image_url_3(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_3));
						contentImage.setSmall_image_url_4(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_4));
						contentImage.setSmall_image_url_5(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_5));
						contentImage.setSmall_image_url_6(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_6));
						contentImage.setSmall_image_url_7(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_7));
						contentImage.setSmall_image_url_8(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_8));
						contentImage.setSmall_image_url_9(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_9));
						contentImage.setSmall_image_url_10(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_10));
						contentImage.setSmall_image_url_11(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_11));
						contentImage.setSmall_image_url_12(resultSet.getString(ContentImagesTB.SMALL_IMAGE_URL_12));
						
						
						list.add(contentImage);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(int contentId,int userId,int imageNo,String imageUrl,String smallUrl){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(ContentImagesTB.CONTENT_ID, contentId);
		insertMap.put(ContentImagesTB.USER_ID,userId);
		insertMap.put("image_url_"+imageNo, imageUrl);
		insertMap.put("small_image_url_"+imageNo, smallUrl);
		
		return sqlMachine.insertRow(ContentImagesTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public ContentImagesTB getByContentId(int contentId){
		
		String sql ="SELECT * FROM " + ContentImagesTB.TABLE_NAME + " WHERE " + ContentImagesTB.CONTENT_ID + " = " + contentId;
		List<ContentImagesTB> list = selectListBySql(sql);
		
		return list.get(0);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public ContentImagesTB getByContentIdOrCreatByUserId(int contentId,int userId){
		
		String sql ="SELECT * FROM " + ContentImagesTB.TABLE_NAME + " WHERE " + ContentImagesTB.CONTENT_ID + " = " + contentId;
		List<ContentImagesTB> list = selectListBySql(sql);
		
		if(list.size() == 0){
			if(insert(contentId,userId,1,"","")){
				list = selectListBySql(sql);
			}
		}
		
		return list.get(0);
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean update(int contentId,int imageNo,String imageUrl,String smallUrl){
		
		//先清空文件
		clear(contentId,imageNo);
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(ContentImagesTB.CONTENT_ID, contentId);
		updateMap.put("image_url_"+imageNo, imageUrl);
		updateMap.put("small_image_url_"+imageNo, smallUrl);
		
		return sqlMachine.updateRow(ContentImagesTB.TABLE_NAME, updateMap, ContentImagesTB.CONTENT_ID, contentId);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean clear(int contentId,int imageNo){
		boolean operationResult = false;
		
		String sql ="SELECT * FROM " + ContentImagesTB.TABLE_NAME + " WHERE " + ContentImagesTB.CONTENT_ID + " = " + contentId;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		String imageUrl = "";
		String smallUrl = "";
		
		if(resultSet != null){
			try {
	
				while(resultSet.next()){
					imageUrl = resultSet.getString("image_url_"+imageNo);
					smallUrl = resultSet.getString("small_image_url_"+imageNo);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(imageUrl != null){
			//删除旧图片
			File oldFile = new File(ServletActionContext.getServletContext().getRealPath(imageUrl));
			File oldSmallFile = new File(ServletActionContext.getServletContext().getRealPath(smallUrl));
			
			
			if(oldSmallFile.delete()){
				if(oldFile.delete()){
					operationResult = true;
				}
			}
		}else{
			operationResult = true;
		}
		
		
		
		return operationResult;
	}
	

	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isExit(int contentId){
		String sql ="SELECT * FROM " + ContentImagesTB.TABLE_NAME + " WHERE " + ContentImagesTB.CONTENT_ID + " = " + contentId;
		List<ContentImagesTB> list = selectListBySql(sql);
		
		if(list.size() >= 1){
			return true;
		}else{
			return false;
		}
		
	}
	
	
}
